//
//  QuestionModel.swift
//  Auscultation
//
//  Created by zwb on 2021/3/31.
//

import Foundation

import HandyJSON

class TestModel:HandyJSON {
    var id = 0
    
    var questions:[QuestionModel] = [QuestionModel]()
    var passScore = ""
    required init() {}
}

class QuestionModel:HandyJSON {
    // 题型 10 单选 20 多选
    var type = "10"
    var id: Int?
    var content: String = ""
    var option : String = ""
    var answer = "" // 答案
    // 答案数组
    var answerArr:[String]?{
        get{
            let data = self.answer.data(using: String.Encoding.utf8)
            if let arr = try? JSONSerialization.jsonObject(with: data!,
                            options: .mutableContainers) as? [String] {
                return arr
            }else{
                return [""]
            }
        }
    }
    
    // 字母排序数组
    var answerSortArr :[String] = []
    
    var answerDic : [String: String]?{
        get{
            let data = self.option.data(using: String.Encoding.utf8)
            if let dict = try? JSONSerialization.jsonObject(with: data!,
                            options: .mutableContainers) as? [String : String] {
                let sortedKeys = Array(dict.keys).sorted(by: <) // ["A", "D", "Z"]

                answerSortArr = sortedKeys
                return dict
                
            }else{
                return ["":""]
            }
        }
    }
    
    //正确答案index数组
    var correctIndexArr: [Int]?{
        get{
            guard let answerArr = answerArr else { return [] }
            var tempArr = [Int]()
            
            for i in answerArr {
                let index = answerSortArr.lgf_IndexOfObject(i)
                tempArr.append(index)
            }
            return tempArr
        }
    }
    
    required init() {}
}
